<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" >
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/myconfig/layui/css/layui.css}" media="all">
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<form>

    <div class="layui-form-item">
        <label class="layui-form-label">计划类型</label>
        <div class="layui-input-block">
            <select id="planType"></select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">关联部门</label>
        <div class="layui-input-block">
            <select id="departmentId"></select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">计划内容</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入计划内容" class="layui-textarea" name="planContent" id="planContent"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block">
            <input type="date" class="layui-input" id="startTime">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block">
            <input type="date" class="layui-input" id="endTime">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="addPlan" lay-filter="addPlanFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script th:src="@{/myconfig/layui/layui.js}"></script>
<script>
    layui.use(['form', 'layedit', 'laydate','upload'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate
            ,upload = layui.upload
            ,$ = layui.$;


        //监听提交
        form.on('submit(addPlanFilter)', function(data){
            var $ = layui.$;
            var planTypeId = $("#planType option:selected").attr("id");
            var departmentId = $("#departmentId option:selected").attr("id");
            //字符串截取
            planTypeId = planTypeId.substring(1);
            departmentId = departmentId.substring(1);
            //时间比较
            var planStartTime = $("#startTime").val();
            var planEndTime = $("#endTime").val();
            if(planEndTime<=planStartTime){
                layer.msg("开始时间和结束时间错误！");
                return;
            }
            $.ajax({
                url: "plan/add",
                type: "post",
                data: {
                    "planTypeId": planTypeId,
                    "planDepartmentId": departmentId,
                    "planContent": $("#planContent").val(),
                    "planStartTime":planStartTime,
                    "planEndTime":planEndTime
                },
                success: function (result,index) {
                    if (result.data =="成功"){
                        layer.msg(result.data,{icon:6},function () {
                            window.parent.location.reload();
                            parent.layer.close(index);
                        })
                    }else{
                        layer.msg("失败",{icon:6},function () {
                            window.parent.location.reload();
                            parent.layer.close(index);
                        })
                    }
                }
            });
            return false;
        });


    });

</script>
<script>
    function addPlan() {
        layui.use('table',function () {
            var $ = layui.$;
            var planTypeId = $("#planType option:selected").attr("id");
            var departmentId = $("#departmentId option:selected").attr("id");
            //字符串截取
            planTypeId = planTypeId.substring(1);
            departmentId = departmentId.substring(1);
            //时间比较
            var planStartTime = $("#startTime").val();
            var planEndTime = $("#endTime").val();
            if(planEndTime<=planStartTime){
                layer.msg("开始时间和结束时间错误！");
            }
            $.ajax({
                url: "plan/add",
                type: "post",
                data: {
                    "planTypeId": planTypeId,
                    "planDepartmentId": departmentId,
                    "planContent": $("#planContent").val(),
                    "planStartTime":planStartTime,
                    "planEndTime":planEndTime
                },
                success: function (result) {
                    if (result.data =="成功"){
                        layer.msg(result.data);
                        window.parent.location.reload();
                        parent.layer.close(index);

                    }else{
                        layer.msg("添加失败")
                    }
                }
            });
    })
    }

    //查询所有的工作计划类型
    function queryAllPlanType() {
        layui.use('table',function () {
            var $ = layui.$;
            $.ajax({
                url: "plan/type/list",
                type: "get",
                success: function (result) {
                    //循环赋值
                   for (var i=0;i < result.data.length;i++){
                       var data = result.data[i];
                       var option = $("<option id='p"+data.planTypeId+"'></option>").text(data.planTypeName);
                       $("#planType").append(option);
                    }
                }
            });
        })
    }
    queryAllPlanType();
    //查询所有事项的id

    function queryAllItem() {
        layui.use('table',function () {
            var $ = layui.$;
            $.ajax({
                url: "plan/department/list",
                type: "get",
                success: function (result) {
                    for (var i=0;i < result.data.length;i++){
                        var data = result.data[i];
                        var option = $("<option id='i"+data.departmentId+"'></option>").text(data.departmentName);
                        $("#departmentId").append(option); }
                }
            });
        })
    }
    queryAllItem();
</script>
</body>
</html>